package com.wy.jetpack.view;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.widget.Chronometer;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class IChronometer extends Chronometer implements LifecycleObserver {

    private long watchedTime;

    public IChronometer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void OnStart(){
       setBase(SystemClock.elapsedRealtime()- watchedTime);
       start();
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private void OnStop(){
        watchedTime =SystemClock.elapsedRealtime()-getBase();
        stop();
    }

}
